import ApiService from '../Model/RequetTool/ApiService';
import Global from '../Model/Global/GCPublic';
import GCStorage from "../Model/DataTool/GCStorage";
/*
 * 首页
 */
// 获取车辆列表
export let GetCarList = (now_page) => {
    return (dispatch) => {
        ApiService.GetCarList(now_page).then((ret) => {
            dispatch({type: 'ActionFunction.GetCarList', result: ret.data,now_page});
        }).catch((err) => {
            dispatch({type: 'ActionFunction.GetCarList', result: null,now_page});
        });
    };
};

// 查询营收、订单、车辆
export let GetHomeInfo = (date_type) => {
    return (dispatch) => {
        ApiService.GetHomeInfo(date_type).then((ret) => {
            dispatch({type: 'ActionFunction.GetHomeInfo', result: ret.data});
        }).catch((err) => {
            dispatch({type: 'ActionFunction.GetHomeInfo', result: null});
        });
    };
};

/*
 * 统计
 */
// 经营分析
export let QueryAnalysis = (date_type, is_shop,now_page) => {
    return (dispatch) => {
        ApiService.QueryAnalysis(date_type, is_shop,now_page).then((ret) => {
            dispatch({type: 'ActionFunction.QueryAnalysis', result: ret.data,now_page});
        }).catch((err) => {
            dispatch({type: 'ActionFunction.QueryAnalysis', result: null,now_page});
        });
    };
};

// 营业统计
export let QueryBusiness = (date_type,is_shop,type) => {
    return (dispatch) => {
        ApiService.QueryBusiness(date_type,is_shop, type).then((ret) => {
            dispatch({type: 'ActionFunction.QueryBusiness', result: ret.data});
        }).catch((err) => {
            dispatch({type: 'ActionFunction.QueryBusiness', result: null});
        });
    };
};

/*
 * 用户中心
 */
// 关于我们
export let AboutUs = () => {
    return (dispatch) => {
        ApiService.AboutUs().then((ret) => {
            dispatch({type: 'ActionFunction.AboutUs', result: ret.data});
        }).catch((err) => {
            dispatch({type: 'ActionFunction.AboutUs', result: null});
        });
    };
};

// 账号密码登录
export let Login = (mobile, password, identity) => {
    return (dispatch) => {
        ApiService.Login(mobile, password, identity).then((ret) => {
            dispatch({type: 'ActionFunction.Login', result: ret.data});
        }).catch((err) => {
            dispatch({type: 'ActionFunction.Login', result: null});
        });
    };
};
export let GetMessageList = (identity,now_page) => {
    return (dispatch) => {
        ApiService.GetMessageList(identity,now_page).then((ret) => {
            dispatch({type: 'ActionFunction.GetMessageList', result: ret.data,now_page});
        }).catch((err) => {
            dispatch({type: 'ActionFunction.GetMessageList', result: null,now_page});
        });
    };
};
export let UserFromStorage = () => {
    return (dispatch) => {
        dispatch({
            type: 'ActionFunction.UserFromStorage',
            id: Global.User.id,
            name: Global.User.name,
            mobile: Global.User.mobile,
            token: Global.User.token,
            business_licence: Global.User.business_licence,
            certificate: Global.User.certificate,
            corporate_bankno: Global.User.corporate_bankno,
            isLoggedIn: true,
            isLoggedOut: false
        });
    }
};

export let UserLogout = () => {
    return (dispatch) => {
        GCStorage.remove(
            Global.Storage.storageUser
        );
        dispatch({
            type: 'ActionFunction.UserLogout',
            isLoggedIn: false,
            isLoggedOut: true,
            id: '',
            name: '',
            mobile: '',
            token: '',
            business_licence: '',
            certificate: '',
            corporate_bankno: ''
        });
    }
};